home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_exmh.idb / usr / freeware / lib / exmh-2.5 / exec.tcl.z / exec.tcl
Text File  |  2002-07-08  |  1KB  |  58 lines

  1. # exec.tcl
  2. #
  3. # wrapper around exec that caches location in your PATH.
  4. # the built-in exec doesn't do this, so the directories on your
  5. # path are constantly searched, which is silly for long lived processes.
  6. #
  7.  
  8. if {[string length [info commands exec-orig]] == 0} {
  9.     rename exec exec-orig
  10.     trace variable env(PATH) w ExecCacheReset
  11. }
  12.  
  13. proc Exec_Init {} {
  14.     # Just to fault in this file.
  15. }
  16.  
  17. proc exec {args} {
  18.     global ExecCache env
  19.  
  20. # Caution:  Enabling the line below will cause PGP passphrases to be logged!
  21. #    Exmh_Debug exec [join $args]
  22.  
  23.     if {![regexp {^(     )*([^<>     ]+)(.*)$} $args all x cmd rest]} {
  24.     # auto-exec generates commands like:
  25.     #    >&@stdout <@stdin /bin/ls
  26.     return [eval {exec-orig} $args]
  27.     }
  28.  
  29.     if {[info exists ExecCache($cmd)]} {
  30.     if [catch {eval {exec-orig $ExecCache($cmd)} $rest} x] {
  31.         if {[info exist ExecCache($cmd)]} {
  32.         unset ExecCache($cmd)
  33.         }
  34.         return -code error $x
  35.     }
  36.     return $x
  37.     } else {
  38.     foreach dir [split $env(PATH) :] {
  39.         set path [file join $dir $cmd]
  40.         if {[file executable $path] && ![file isdirectory $path]} {
  41.         if [catch {eval {exec-orig $path} $rest} x] {
  42.             return -code error $x
  43.         }
  44.         set ExecCache($cmd) $path
  45.         return $x
  46.         }
  47.     }
  48.     }
  49.     eval {exec-orig $cmd} $rest
  50. }
  51.  
  52. proc ExecCacheReset {args} {
  53.     global ExecCache env
  54.     if {[info exist ExecCache]} {
  55.     unset ExecCache
  56.     }
  57. }
  58.